﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace Jy123.Web.Manage.User.Guess
{
    public partial class Guess : MgConfig
    {
        protected string settlementHtml = "";
        protected int pagesize = 10;
        protected int cpage = 1;//当前页
        protected List<int?> count;
        

        public static string KeyValue = "";
        protected string sort = "Guess";
        Jy123.Model.User_Guess_Main modelGuess;
        Jy123.BLL.User_Guess_Stat bllStat = new Jy123.BLL.User_Guess_Stat();

        protected void Page_Load(object sender, EventArgs e)
        {
            //int userID;
            //string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            //if (string.IsNullOrEmpty(strUserIdFromCookie))
            //{
            //    Response.Redirect("/");
            //}
            //userID = int.Parse(strUserIdFromCookie);

            string type="";
            if (!string.IsNullOrEmpty(Request.QueryString["Type"]))
            {
                 type = Request.QueryString["Type"];
            }
            if (!string.IsNullOrEmpty(Request.QueryString["page"]))
            {
                cpage = Convert.ToInt32(Request.QueryString["page"]);
            }
            
            if (type == "del")//删除
            {
                modelGuess = new BLL.User_Guess_Main().GetModel(Convert.ToInt16(Request.QueryString["GuessID"]));

                if (modelGuess.IsSettlement || !modelGuess.IsCheckup)//已结算或者未审核的竞猜
                {
                    new Jy123.BLL.Guess.GuessSql().DeleteByWhere("User_Guess_Stat", "FGuess in " + modelGuess.ID.ToString());//删除相关竞猜统计

                    new Jy123.BLL.Guess.GuessSql().DeleteByWhere("User_Guess_Review", "FGuess in " + modelGuess.ID.ToString());//删除相关回复

                    new Jy123.BLL.User_Guess_Main().Delete(modelGuess.ID);//删除竞猜
                }
                
                ShowGuessList();
            }
            else if (type == "sett")
            {
                modelGuess = new BLL.User_Guess_Main().GetModel(Convert.ToInt32(Request.QueryString["GuessID"]));
                
                new BLL.Guess.GuessSql().guessSettlement(modelGuess);

                ShowGuessList();
            }
            else if (type == "settAll")
            {
                List<Jy123.Model.User_Guess_Main> guessList = new BLL.User_Guess_Main().GetModelList("IsSettlement = 0 and CONVERT(varchar(10), OpenTime, 120) = CONVERT(varchar(10), GETDATE(), 120)");

                foreach(Jy123.Model.User_Guess_Main guess in guessList)
                {
                    new BLL.Guess.GuessSql().guessSettlement(guess);
                }
                ShowGuessList();
            }
            else
            {
                if (!IsPostBack)
                {
                    ShowGuessList();
                }
            }

        }
        /// <summary>
        /// 加载竞猜信息
        /// </summary>
        private void ShowGuessList()
        {
            int s = 10;
            if (!string.IsNullOrEmpty(Request.QueryString["s"])) 
            {
                s = Convert.ToInt32(Request.QueryString["s"]);
            }
            //if (!string.IsNullOrEmpty(""))//搜索框不为空
            //{
            //    //Rpt_GuessList.DataSource = bllGuess.GetList("email like '%" + Key.Text + "%'");
            //    //Rpt_GuessList.DataSource = new BLL.Guess.GuessSql().GetGuessList("email like '%" + Key.Text + "%'", cpage, pagesize, out count);
            //    //Rpt_GuessList.DataBind();
            //}
            //else //搜索框为空
            //{
                if (s == 0)   //所有未审核的竞猜
                {
                    Rpt_GuessList.DataSource = new BLL.Guess.GuessSql().GetGuessList("IsCheckup = 0", cpage, pagesize, out count);
                    Rpt_GuessList.DataBind();
                }
                else if (s == 1)//当天待结算的竞猜
                {
                    Rpt_GuessList.DataSource = new BLL.Guess.GuessSql().GetGuessList("IsSettlement = 0 and CONVERT(varchar(10), OpenTime, 120) = CONVERT(varchar(10), GETDATE(), 120)", cpage, pagesize, out count);
                    Rpt_GuessList.DataBind();
                }
                else if (s == 2)//当天待结算的竞猜
                {
                    string userID= Request.QueryString["uid"];
                    Rpt_GuessList.DataSource = new BLL.Guess.GuessSql().GetGuessList("Fuser =" + userID, cpage, pagesize, out count);
                    Rpt_GuessList.DataBind();
                }
                else
                {
                    Rpt_GuessList.DataSource = new BLL.Guess.GuessSql().GetGuessList("",cpage,pagesize,out count);
                    Rpt_GuessList.DataBind();
                }

            //}
        }

        /// <summary>
        /// 转换状态数值为中文意思
        /// </summary>
        /// <param name="status">状态数值</param>
        /// <returns></returns>
        protected string ConvertStatus(object ID)
        {
            Jy123.Model.User_Guess_Main guess = new BLL.User_Guess_Main().GetModel(Convert.ToInt32(ID));

            settlementHtml = "";
            if (!guess.IsCheckup)
                return "未审核";
            else if (DateTime.Compare(DateTime.Now, guess.OpenTime) < 0 && guess.IsCheckup)
                return "竞猜中";
            else if (DateTime.Compare(DateTime.Now, guess.OpenTime) < 0 && DateTime.Compare(DateTime.Now, guess.OverTime) > 0 && guess.IsCheckup && !guess.IsSettlement)
            {
                settlementHtml = "<a class=\"btnsty\" href=\"Gues.aspx?Type=Settlement&GuessID=" + ID + "\">结算</a>";
                return "待结算";
            }
            else if (DateTime.Compare(DateTime.Now, guess.OpenTime) > 0 && !guess.IsSettlement)
            {
                settlementHtml = "<a class=\"btnsty\" href=\"Gues.aspx?Type=Settlement&GuessID=" + ID + "\">结算</a>";
                return "逾期未结算";
            }
            else if (DateTime.Compare(DateTime.Now, guess.OpenTime) > 0 && guess.IsCheckup && guess.IsSettlement)
                return "已结束";

            return "未知状态";
        }

        /// <summary>
        /// 获取发布用户
        /// </summary>
        /// <param name="ID">用户ID</param>
        /// <returns>用户名</returns>
        protected string GetUserName(object ID)
        {
            Jy123.Model.User_Main model = new Jy123.Model.User_Main();

            Jy123.BLL.User_Main bll = new Jy123.BLL.User_Main();

            model = bll.GetModel(Convert.ToInt32(ID));

            return model.UserName;
        }

        /// <summary>
        /// 获取统计HTML
        /// </summary>
        /// <param name="ID">竞猜ID</param>
        /// <returns>统计HTML</returns>
        protected string GetStat(object ID)
        {
            StringBuilder sbHtml = new StringBuilder();
            string html = "<a href=\"StatInfo.aspx?id=" + ID + "\">{0}</a>";

            if (new Jy123.BLL.Guess.GuessSql().StatExists("User_Guess_Stat", ID))
            {

                sbHtml.AppendFormat(html, new Jy123.BLL.Guess.GuessSql().GetStatHtml(ID));
                return sbHtml.ToString();
            }
            else
            {
                sbHtml.Append("<a href=\"javascript:\">(0人/0份)</a>");
                return sbHtml.ToString();
            }
        }


        /// <summary>
        /// 获取回复数
        /// </summary>
        /// <param name="ID">竞猜ID</param>
        /// <returns>回复数HTML</returns>
        protected string GetReview(object ID)
        {
            StringBuilder sbHtml = new StringBuilder();
            string count=new Jy123.BLL.Guess.GuessSql().GetReviewCount(ID);

            if (new Jy123.BLL.Guess.GuessSql().StatExists("User_Guess_Review", ID))  //是否有回复
            {
                sbHtml.AppendFormat("<a href=\"ReviewInfo.aspx?id=" + ID + "\">{0}</a>", count);
                return sbHtml.ToString();
            }
            else
            {
                sbHtml.Append("<a href=\"javascript:\">0</a>");
                return sbHtml.ToString();
            }
        }

        /// <summary>
        /// 获取结算按钮
        /// </summary>
        /// <param name="id">竞猜ID</param>
        /// <returns>结算按钮HTML</returns>
        protected string getButton(object ID)
        {
            Jy123.Model.User_Guess_Main guess = new BLL.User_Guess_Main().GetModel(Convert.ToInt32(ID));
            
            string strHtml= "";
            if (!guess.IsCheckup)
            {
                strHtml = "<a class=\"btnsty\" href=\"GuessCheckUp.aspx?GuessID=" + guess.ID + "\">审核</a>";
                strHtml = strHtml + "<a class=\"btnsty\" href=\"GuessEdit.aspx?GuessID=\">编辑</a>";
                strHtml = strHtml + "<a class=\"btnsty cred\" href=\"javascript:del(" + guess.ID + "," + cpage + ");\">删除</a>";
                return strHtml;
            }
            if (DateTime.Now.ToShortDateString().Equals(guess.OpenTime.ToShortDateString()) && guess.IsCheckup && !guess.IsSettlement)
            {
                strHtml = "<a class=\"btnsty\" href=\"Guess.aspx?type=sett&GuessID=" + guess.ID + "&page=" + cpage + "\">结算</a>";
                return strHtml;
            }
            if (guess.IsCheckup && !guess.IsSettlement && DateTime.Compare(DateTime.Now,guess.OverTime)>0)
            {
                strHtml = "<a class=\"btnsty\" href=\"Guess.aspx?type=sett&GuessID=" + guess.ID + "&page=" + cpage + "\">强制结算</a>";
                return strHtml;
            }
            if (guess.IsSettlement)
            {
                strHtml = "<a class=\"btnsty cred\" href=\"javascript:del(" + guess.ID + "," + cpage + ");\">删除</a>";
            }
            return strHtml;
            
        }
    }
}
